package com.wxx.controller;

import com.wxx.model.Task;
import com.wxx.util.QuartzUtils;
import com.wxx.util.ScheduleUtils;


import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Annotion:  对job进行动态增删改查接口示例
 * @ClassName: QuartzController
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private ScheduleUtils scheduleUtils;

    /**
     * 添加一个新任务（之前不存在任务）
     * @param j
     * @return
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    @PostMapping("/addTask")
    public Object addTask(Task j) throws ClassNotFoundException, SchedulerException {
//        Task j = new Task();
//        j.setCronExpression("0/3 * * * * ?");
//        j.setJobName("sea");
//        j.setJobGroup("wxx");
//        j.setJobStatus("1");
//        j.setParameters(null);
//        j.setJobClass("com.wxx.Task1");
//        j.setDescription("54645654646464.");
//        j.setDruable(true);
//        try {
//            boolean b = QuartzUtils.addJob(j);
//            System.out.println("添加job成功!");
//            return b;
//        } catch (Exception e) {
//            System.out.println("添加job异常!");
//            e.printStackTrace();
//        }
        boolean b = QuartzUtils.addJob(j);
        return b;
    }

    /**
     * 删除任务
     * @param jobName
     * @param jobGroup
     * @return
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    @PostMapping("/deleteJob")
    public Object deleteJob(String jobName, String jobGroup) throws SchedulerException {
        boolean b = QuartzUtils.deleteJob(jobName, jobGroup);
        return b;
    }

    /**
     * 更新任务运行的的时间
     * @param jobName
     * @param jobGroup
     * @param cronExpression
     * @return
     */
    @PostMapping("/updateJob")
    public Object updateJob(String jobName, String jobGroup,String cronExpression)  {
        boolean b = QuartzUtils.updateJob(jobName, jobGroup,cronExpression);
        return b;
    }

    /**
     * 停止一个任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("/stopJob")
    public Object stopJob(String jobName, String jobGroup) throws SchedulerException {
        boolean b = QuartzUtils.stopJob(jobName, jobGroup);
        return b;
    }

    /**
     * 恢复指定任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("/restartJob")
    public Object restartJob(String jobName, String jobGroup) throws SchedulerException {
        boolean b = QuartzUtils.restartJob(jobName, jobGroup);
        return b;
    }

    /**
     * 暂停所有job
     */
    @RequestMapping("/stopAll")
    public Object stopAll() throws SchedulerException {
        boolean b = QuartzUtils.stopAll();
        return b;
    }

    /**
     * 重启所有job
     */
    @RequestMapping("/restartAll")
    public Object restartAll() throws SchedulerException {
        boolean b = QuartzUtils.restartAll();
        return b;
    }


}
